<?php
/********url地址,logo地址的正则判断还没做*********/
include 'D:/dingjiangbbs/common/home.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);

/****************添加数据开始**********************/


if ($_POST['caozuo'] == '添加') {

	if (empty($_POST['orderby']) || empty($_POST['site_name']) || empty($_POST['site_URL'])) {

		exit('添加内容前三项不得为空');

	}

	//获取post传过来的值(一条记录)
	//var_dump($_POST);
	$orderby    = $_POST['orderby'];
	$site_name  = $_POST['site_name'];
	$site_URL   = $_POST['site_URL'];
	$font_tip   = $_POST['font_tip'];
	$logo_place = $_POST['logo_place'];

	//判断显示顺序是否为数值
	if (!is_numeric($orderby) || (strlen($orderby)>2 || strlen($orderby)<1)) {

		exit('显示顺序框中内容不得为空,且只能填入1~2位阿拉伯数字');
	}

	//将数据插入数据库
	$table = DB_TABLE_LINK;
	$data  = [
		'displayorder' => $orderby,
		'name'		   => "$site_name",
		'url' 	   	   => "$site_URL",
		'description'  => "$font_tip",
		'logo' 		   => "$logo_place",
		'addtime'      => time()
	];

	$res_link1 = insert($link,$table,$data);
	//var_dump($res_link1);

	//输出
	if (!$res_link1) {
		exit('<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">添加失败,返回继续</a>');
	}

	echo '<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">添加成功,返回继续</a>';
}

/****************添加数据结束**********************/

/****************删除数据开始**********************/

if ($_POST['caozuo'] == '删除') {
	
	if (empty($_POST['$arr'])) {

		exit('未勾选内容,请勾选选项再操作');
	}

	//取得 lid的数组(数组)
	$arr = $_POST['$arr'];
	$id = implode(',',$arr);	
	//var_dump($id);

	
	//删除数据库数据
	$table  = DB_TABLE_LINK;
	$where  = "lid in ($id)";
	$res_link2 = del($link,$table,$where);
	//var_dump($res_link2);

	if (!$res_link2) {

		exit('<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">删除失败,返回继续</a>');
	}
	echo '<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">删除成功,返回继续</a>';
}

/****************删除数据结束**********************/

/****************修改数据开始**********************/
//var_dump($_POST);

if ($_POST['caozuo'] == '修改') {
	
	//var_dump($_POST);

	if (empty($_POST['$arr'])) {

		exit('未勾选内容,请勾选选项再操作');
	}

	//获取post传的值(数组)
	$arr = $_POST['$arr'];
	$orderby    = $_POST['$orderby'];
	$site_name  = $_POST['$site_name'];
	$site_URL   = $_POST['$site_URL'];
	$font_tip   = $_POST['$font_tip'];
	$logo_place = $_POST['$logo_place'];

	//修改数据库数据
	for ($i=0;$i<count($arr);$i++) {

		
		$lid   = $arr[$i];
		//var_dump($lid);
		$table = DB_TABLE_LINK;
		
		$data  = [

			//id下标和取值下标 一定要一致 【关键】
			'displayorder' => $orderby[$lid],
			'name'		   => "$site_name[$lid]",
			'url' 	   	   => "$site_URL[$lid]",
			'description'  => "$font_tip[$lid]",
			'logo' 		   => "$logo_place[$lid]",
			'addtime'      => time()
		];

		$where  = "lid=$lid";
		//$where = "lid in ($id)";
		$res_link3 = update($link,$table,$data,$where);
		//var_dump($res_link3);
	}

	//输出,跳转页面
	if (!$res_link3) {
			exit('<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">修改失败,返回继续</a>');
		}
	echo '<a href="'.WEB_SITE.'admin_cg.php?bighead=1&cg_number=2">修改成功,返回继续</a>';
}
/****************修改数据结束**********************/

//关闭资源
mysqli_close($link);
